home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
TCL1
/
CTCPSTRE
/
CTCPSTRE.C
next >
Wrap
Text File
|
1990-06-28
|
7KB
|
320 lines
/*
*** tcplib ***: TCP synchronous stream management utilities.
Written by Matt Mashyna (Mostly) and Matt McNally,
with original portions donated by Rob Chandok.
All sorces ⌐ 1989 Carnegie Mellon University.
Created: 08/29/89 Last Revised: 09/08/89 - Mashyna
Adapted to Think C Objects 10/10/89 - Mashyna
*/
#include <Global.h>
#include <Commands.h>
#include <CApplication.h>
#include <CBartender.h>
#include <CDataFile.h>
#include <CDecorator.h>
#include <CDesktop.h>
#include <CError.h>
#include <stdlib.h>
#include <stdio.h>
#include "CTCPStream.h"
#define TIME_OUT 10 /* default timeout period */
short drvrRefNum = 0;
/*****
*
* Asynchonous event handler. Make your own -- not a method, just an example.
*
****/
void ASR(tcpStream, eventCode, userDataPtr, terminReason, icmpMsg)
StreamPtr tcpStream;
unsigned short eventCode;
Ptr userDataPtr;
unsigned short terminReason;
struct ICMPReport *icmpMsg;
{
printf("something happened: Eventcode %d\n",eventCode);
fflush(stdout);
Activity = eventCode; /* tell the object that something happened */
}
/*****
*
* needed for host resolution.
*
****/
pascal void dnrDone(struct hostInfo *hostInfoPtr, char *userDataPtr)
{
#pragma unused(hostInfoPtr)
(*userDataPtr)++;
}
/*****
*
* returns a pointer to a resolved name from an address.
*
****/
char *inet_ntoa(ip_addr addr)
{
char dnrdoneflag;
static struct hostInfo hi;
OSErr err;
dnrdoneflag = 0;
err = AddrToName(addr,&hi,&dnrDone,&dnrdoneflag);
if (err == cacheFault) {
while (!dnrdoneflag) {
/* SystemTask? WaitNextEvent ? */
}
err = hi.rtnCode;
}
if (err == noErr) {
return hi.cname;
}
/* if all else fails */
if ((err = AddrToStr(addr,hi.cname)) != noErr) {
/* doDNRerr(err); */
return "??";
} else {
return hi.cname;
}
}
/*******************************************
* tcplib utilities - *
* bzero *
*******************************************/
void bzero(char * bytes, int len)
{
register char *p;
for (p = bytes; len > 0; len--,p++) {
*p = '\0';
}
} /*** end of bzero ***/
/*******************************************
* Release - *
* Release & close a stream *
*******************************************/
void CTCPStream::Release()
{
TCPiopb tmp;
char *buffPtr;
bzero((char *)&tmp,sizeof(tmp));
tmp.csCode = TCPRelease;
tmp.tcpStream = tcppb.tcpStream;
tmp.ioCRefNum = tcppb.ioCRefNum;
ourError = PBControl((ParmBlkPtr)&tmp,false);
if(!ourError) {
buffPtr = tmp.csParam.create.rcvBuff;
if(tmp.csParam.create.rcvBuffLen) free(buffPtr);
}
ourError = MemError();
} /*** end of TCPReleaseStream ***/
void CTCPStream::Dispose()
{
Release();
inherited::Dispose();
}
/*******************************************
* State - *
* check a stream state *
*******************************************/
int CTCPStream::State()
{
TCPiopb tmp;
int state = 0;
bzero((char *) &tmp,sizeof(tmp));
if (tcppb.tcpStream != 0) {
tmp.csCode = TCPStatus;
tmp.tcpStream = tcppb.tcpStream;
tmp.ioCRefNum = tcppb.ioCRefNum;
ourError = PBControl((ParmBlkPtr)&tmp,false);
if (ourError != noErr)
return(ourError);
state = (int) tmp.csParam.status.connectionState;
}
return(state);
} /*** end of TCPStreamState ***/
/*******************************************
* Close - *
* close a tcp connection; *
*******************************************/
void CTCPStream::Close()
{
ourError = noErr;
if (tcppb.tcpStream != 0) {
tcppb.ioCompletion = NIL;
tcppb.csCode = TCPClose;
tcppb.csParam.create.userDataPtr = NIL;
ourError = PBControl((ParmBlkPtr)&tcppb,false);
} /* if */
} /*** end of CloseTCPStream ***/
/*******************************************
* ITCPStream - *
* create a tcp connection; call this first *
*******************************************/
void CTCPStream::ITCPStream(short size)
{
IOBuffer = malloc(size); /* allocate a buffer for the connection */
if (IOBuffer == NULL)
ourError = MemError();
timeout = TIME_OUT;
async = false;
Activity = 0;
tcppb.csCode = TCPCreate;
tcppb.ioCompletion = NIL;
tcppb.ioCRefNum = drvrRefNum;
tcppb.csParam.create.notifyProc = NIL;
tcppb.csParam.create.rcvBuffLen = size;
tcppb.csParam.create.rcvBuff = IOBuffer;
ourError = PBControl((ParmBlkPtr)&tcppb,false);
} /*** end of CreateTCPStream ***/
/*************************************************
* Listen - [for ╘incoming╒ packets] *
* listen for a tcp connection; call this second *
* *
* port is the port to listen on ie 79 is finger *
**************************************************/
void CTCPStream::Listen(tcp_port port)
{
tcppb.ioCompletion = NIL;
tcppb.csCode = TCPPassiveOpen;
tcppb.csParam.open.remoteHost = 0;
tcppb.csParam.open.remotePort = 0;
tcppb.csParam.open.localPort = thePort = port;
ourError = PBControl((ParmBlkPtr)&tcppb,true);
} /*** end of ListenTCPSteam ***/
/********************************************
* Open - [for ╘outgoing╒ packets] *
* open a tcp connection; call this second *
* *
* host is a C string of the host to open. *
********************************************/
void CTCPStream::Open(tcp_port port,char *host)
{
char dnrdoneflag;
ourError = StrToAddr(host,&hp,&dnrDone,&dnrdoneflag);
if (ourError == cacheFault) {
while (!dnrdoneflag) {
/* SystemTask? WaitNextEvent ? */
} /* while */
} /* if */
tcppb.ioCompletion = NIL;
tcppb.csCode = TCPActiveOpen;
tcppb.csParam.open.remoteHost = hp.addr[0];
tcppb.csParam.open.remotePort = thePort = port; /* eg. finger is 73 */
ourError = PBControl((ParmBlkPtr)&tcppb,false);
} /*** end of OpenTCPSteam ***/
/*******************************************
* Write - *
* write to a tcp Stream *
*******************************************/
void CTCPStream::Write(char *srcBuff,int size)
{
tcppb.csCode = TCPSend;
tcppb.csParam.send.ulpTimeoutValue = timeout;
tcppb.csParam.send.ulpTimeoutAction = 1;
/* build write-data structure */
wds[0].length = size;
wds[0].ptr = srcBuff;
wds[1].length = 0;
tcppb.csParam.send.wdsPtr = (Ptr)&wds;
ourError = PBControl((ParmBlkPtr)&tcppb,async);
} /*** end of WriteTCPStream ***/
/*******************************************
* Read - *
* read from a tcp Stream *
*******************************************/
void CTCPStream::Read(char *targBuff,int *size)
{
tcppb.csCode = TCPRcv;
tcppb.csParam.receive.commandTimeoutValue = timeout; /* minimum time */
tcppb.csParam.receive.secondTimeStamp = 0;
tcppb.csParam.receive.rcvBuff = targBuff;
tcppb.csParam.receive.rcvBuffLen = *size;
ourError = PBControl((ParmBlkPtr)&tcppb,async);
*size = tcppb.csParam.receive.rcvBuffLen;
} /*** end of ReadTCPStream ***/
/*******************************************
* SetTimeOut - *
* set the timeout value for reads & writes *
********************************************/
void CTCPStream::SetTimeOut(byte secs)
{
timeout = secs;
}